package com.otod.office;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.constants.AttachmentConstant;
import com.otod.commons.constants.PropertiesConstant;
import com.otod.commons.tools.UserEnv;
import com.otod.commons.utils.RandomUtil;
import com.otod.commons.utils.SpringContextHolder;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.pm.prj.service.StepService;
import com.otod.sys.file.service.IFileService;
import com.otod.sys.vo.FileVo;
@Controller
@RequestMapping("office/region")
public class NewWordAndSaveController extends BaseController{
	
	@Autowired
	private IFileService fileService;
	
	@Autowired
	private StepService stepService;
	
	private static String PATH = UserEnv.getProp(PropertiesConstant.UPLOAD_FILE_PATH);
	
	
	private String message="";
	
	public String getMessage(){
    	return message;
    }
	
	/**
	 * 文档合并操作
	 * @param request 文档数量，及先后顺序
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "create", method = RequestMethod.GET)
	@ResponseBody
	public String dataRegionCreate(HttpServletRequest request) throws AppException {
		
		String empNo =  SessionUserUtil.getCurrentUser().getUserNo();
		String prjId = request.getParameter("prjId");
		String wordIds = request.getParameter("wordIds");
		String stepId = request.getParameter("stepId");
		if(StringUtils.isEmpty(wordIds)){
			throw new AppException("未选择文档，请重新选择");
		}
		String[] ids = wordIds.split(",");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("wordIds", ids);
		List<FileVo> fileList = fileService.queryForListByDocIds(map);

		IFileService busiFileInfo = (IFileService)SpringContextHolder.getBean(IFileService.BEAN_ID); 
		FileVo bo = new FileVo();
		List<FileVo> newFileList = new ArrayList<FileVo>();
		for (int i = 0;i<fileList.size();i++) {
			String id = RandomUtil.getUUID();
			bo = fileList.get(i);
			bo.setUuid(id);
			
			if(StringUtils.isEmpty(stepId)){
				bo.setBusitableKey(AttachmentConstant.ATTACH_EMPTYBUSIKEY);
			}else{
				bo.setBusitableKey(stepId);
				bo.setProjectId(prjId);
			}
			//此处需要添加当前任务ID--参数传递获得
			this.setInsertOperInfo(bo);
			newFileList.add(bo);							
		}
		fileList.clear();
		map.clear();
		map.put("busitableKey", stepId);
		map.put("prjectId", prjId);
		fileList = busiFileInfo.queryforList(map);
		busiFileInfo.insert(newFileList,fileList);
		return JsonSuccess.factory("加入成果文档成功！");
	}
	
	@RequestMapping(value = "createNewWord", method = RequestMethod.GET)
	public String createNewWord(HttpServletRequest request, Model model) {
		
		String empNo =  SessionUserUtil.getCurrentUser().getUserNo();
		
		/*PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
		poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); //此行必须
		//隐藏菜单栏
		poCtrl1.setMenubar(false);
		//隐藏工具栏
		poCtrl1.setCustomToolbar(false);
		//设置保存页面
//		poCtrl1.setSaveFilePage("office/createWord/SaveNewFile");
		poCtrl1.setSaveFilePage("SaveNewWord.do");
		
		poCtrl1.setCaption("妙策科技office控件");
		//新建Word文件，webCreateNew方法中的两个参数分别指代“操作人”和“新建Word文档的版本号”
		poCtrl1.webCreateNew(empNo,DocumentVersion.Word2007);
		poCtrl1.setTagId("PageOfficeCtrl1"); //此行必须	
*/		return "office/createWord/CreateWord";
		
	}
	
	@RequestMapping(value = "SaveNewWord")
	public String SaveNewWord(HttpServletRequest request, HttpServletResponse response,Model model) throws AppException {
		
		//定义保存对象import com.otod.commons
		//FileSaver fs = new FileSaver(request, response);
		//保存文件
		
		
		//String FileSubject = fs.getFormField("FileSubject").trim();
		
		/*if(FileSubject.equals("请输入文档主题")){
			throw new AppException("文档主题不能为空！");
		}*/
		
		String PATH = UserEnv.getProp(PropertiesConstant.UPLOAD_FILE_PATH);
		String path = PATH + "document" + File.separator;
		
		File pathFile = new File(path);
		boolean isPathExist = pathFile.exists();
		if (!isPathExist)
		     pathFile.mkdirs();
		//fs.saveToFile(path + FileSubject+fs.getFileExtName());
		//设置保存结果
		IFileService busiFileInfo = (IFileService)SpringContextHolder.getBean(IFileService.BEAN_ID); 
		String id = RandomUtil.getUUID();
		FileVo bo = new FileVo();
			bo.setUuid(id);
			/*bo.setFileName(FileSubject+fs.getFileExtName());
			//bo.setFileType(funcType);											//功能类型
			bo.setFileFormat(fs.getFileExtName());
			bo.setFileSize(BigDecimal.valueOf(fs.getFileSize()));
			//bo.setDepict(StringUtils.isEmpty(smartRequest.getParameter("fileDesc"))?"无":smartRequest.getParameter("fileDesc"));
			bo.setFileServerPath(path);
			bo.setFileServerName(fs.getFileName());*/
			//新增时，附件的业务ID还没有生成，先传成默认KEY，需要保存成功时更新
			bo.setBusitableKey(AttachmentConstant.ATTACH_EMPTYBUSIKEY);
			//此处需要添加当前任务ID--参数传递获得
			this.setInsertOperInfo(bo);
			busiFileInfo.insert(bo);							
		
		//	fs.showPage(300, 300);
	    	
			model.addAttribute("message", "保存成功");
			//model.addAttribute("FileSaver", fs);
			
	    	return "office/createWord/SaveNewFile";
		
	}
	
	

}
